using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LogFileViewer
{
    public partial class PerformanceSettings : Form
    {
        public PerformanceSettings()
        {
            InitializeComponent();
            initSettings();
        }

        private Properties.Settings sets = Properties.Settings.Default;

        private void initSettings()
        {
            switch (sets.HandleFileHandling)
            {
                case (int)DataAccess.FileHandling.SingleFileHandling:
                    this.rdbHandlingSingle.Checked = true;
                    this.rdbHandlingMulti.Checked = false;
                    break;
                case (int)DataAccess.FileHandling.MultiFileHandling:
                    this.rdbHandlingSingle.Checked = false;
                    this.rdbHandlingMulti.Checked = true;
                    break;
                default:
                    break;
            }

            switch (sets.HandleDataManagement)
            {
                case (int)DataAccess.DataHandling.DataBuffering:
                    this.rdbDataManageBuffering.Checked = true;
                    this.rdbDataManageIndexing.Checked = false;
                    break;
                case (int)DataAccess.DataHandling.LineIndexing:
                    this.rdbDataManageBuffering.Checked = false;
                    this.rdbDataManageIndexing.Checked = true;
                    break;
                default:
                    break;
            }

            switch (sets.HandleReadMethod)
            {
                case (int)DataAccess.ReaderHandling.ReadFull:
                    this.rdbMethodFullRead.Checked = true;
                    this.rdbMethodActiveTypes.Checked = false;
                    this.rdbMethodActiveRows.Checked = false;
                    break;
                case (int)DataAccess.ReaderHandling.ActiveTypes:
                    this.rdbMethodFullRead.Checked = false;
                    this.rdbMethodActiveTypes.Checked = true;
                    this.rdbMethodActiveRows.Checked = false;
                    break;
                case (int)DataAccess.ReaderHandling.ActiveRows:
                    this.rdbMethodFullRead.Checked = false;
                    this.rdbMethodActiveTypes.Checked = false;
                    this.rdbMethodActiveRows.Checked = true;
                    break;
                default:
                    break;
            }

            setTooltips();
        }

        private void setTooltips()
        {
            // Create the ToolTip and associate with the Form container.
            ToolTip toolTip = new ToolTip();

            // Set up the delays for the ToolTip.
            toolTip.AutoPopDelay = 5000;
            toolTip.InitialDelay = 1000;
            toolTip.ReshowDelay = 500;
            // Force the ToolTip text to be displayed whether or not the form is active.
            toolTip.ShowAlways = true;

            // Set up the ToolTip text for the widgets
            toolTip.SetToolTip(this.rdbHandlingSingle, "Load the untouched log file.\nNo changes are performed.");
            toolTip.SetToolTip(this.rdbHandlingMulti, "Split the log file into the 5 types:\nINFO, DEBUG, WARN, ERROR, FATAL.\nThis is a cheat to speed up access because\nonly the file with the selected type is loaded.");
            toolTip.SetToolTip(this.rdbMethodFullRead, "Read the full log file at once.\nThis should be used for small to medium size log files.\nWhen loading bigger files, buffer overflow can occur.");
            toolTip.SetToolTip(this.rdbMethodActiveTypes, "Read log entries of the selected type(s) only.\nUsing this option is a compromise between display speed and possible file size.");
            toolTip.SetToolTip(this.rdbMethodActiveRows, "Read required parts of the log file only.\nCould be useful when handling huge log files.\nNote that this mode depends on your hard drive access speed.");
            toolTip.SetToolTip(this.rdbDataManageBuffering, "Buffer the whole log file contents in memory.\nThis speeds up performance for normal size log files.");
            toolTip.SetToolTip(this.rdbDataManageIndexing, "Buffer the indexes of the log file only.\nCould be needed when handling huge log files.");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // FILE HANDLING
            if (rdbHandlingSingle.Checked)
            {
                sets.HandleFileHandling = (int)DataAccess.FileHandling.SingleFileHandling;
            }
            else if (rdbHandlingMulti.Checked)
            {
                sets.HandleFileHandling = (int)DataAccess.FileHandling.MultiFileHandling;
            }

            // READ METHOD
            if (rdbMethodFullRead.Checked)
            {
                sets.HandleReadMethod = (int)DataAccess.ReaderHandling.ReadFull;
            }
            else if (rdbMethodActiveTypes.Checked)
            {
                sets.HandleReadMethod = (int)DataAccess.ReaderHandling.ActiveTypes;
            }
            else if (rdbMethodActiveRows.Checked)
            {
                sets.HandleReadMethod = (int)DataAccess.ReaderHandling.ActiveRows;
            }

            // DATA MANAGEMENT
            if (rdbDataManageBuffering.Checked)
            {
                sets.HandleDataManagement = (int)DataAccess.DataHandling.DataBuffering;
            }
            else if (rdbDataManageIndexing.Checked)
            {
                sets.HandleDataManagement = (int)DataAccess.DataHandling.LineIndexing;
            }

            // Saving everything
            sets.Save();

            // Closing settings form
            this.Close();
        }

    }
}